package com.testing.sunnycommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testing.sunnycommon.mapper.SunnyUserMapper;
import com.testing.sunnycommon.model.SunnyUser;
import com.testing.sunnycommon.service.SunnyUserService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Classname SunnyUserServiceImpl
 * @Description 类型说明
 * @Date 2023/6/28 21:56
 * @Created by 特斯汀Roy
 */
@Service
public class SunnyUserServiceImpl extends ServiceImpl<SunnyUserMapper, SunnyUser> implements SunnyUserService {

    @Override
    public SunnyUser loadUserByUsername(String username) throws UsernameNotFoundException {
        SunnyUser user = getOne(new LambdaQueryWrapper<SunnyUser>().eq(SunnyUser::getUsername, username));
        return user;
    }

    @Override
    public boolean isImage(InputStream stream) {
        try {
            BufferedImage read = ImageIO.read(stream);
            //读取到的是非空图片文件
            return read != null;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
